<template>
  <div>
    <!--条件筛选-->
    <TopFilter :list="topFilterList" :cols="3" :collapse="false" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <!--操作 -->
    <button-area :style="{ paddingLeft: '80px' }"></button-area>
    <!-- 列筛选-->
    <FilterTable
      ref="table"
      columnsRef="receiveStuff"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :isExportExcel="true"
      exportFileName="配件前台领料查询.xlsx"
      :isSelectColumn="isSelectColumn"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectData"
    >
      <!--列操作-->
      <template slot="controls"> </template>
    </FilterTable>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import moment from 'moment';
import { arrayObjDeWeight } from '@/utils/index';
import dictCode from '@/utils/dictCode';
import { searchRoleByCode } from '@/api/common/empSelect/index';
import { searchPartFrontInfo } from '@/api/part/report/receiveStuff';
// 翻译人员ID
import { getUserListSelect } from '@/api/report/repair/repairreport';
// 翻译车系ID
import { getSeries } from '@/api/customerRepair/customerCare/insuranceDueRemind/index';

export default {
  name: 'LHMQueryAllMaterial',
  components: {},
  mixins: [dictionary],
  data() {
    return {
      topFilterList: this.createTopFilterList(),
      fetchParams: {
        outStr:
          moment(new Date())
            .subtract(1, 'weeks')
            .format('YYYY-MM-DD') + ' 00:00:00',
        outEnd: moment(new Date()).format('YYYY-MM-DD') + ' 23:59:59'
      },
      visible: false,
      isSelectColumn: false,
      dialogTitle: '',
      fetchApi: searchPartFrontInfo,
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      tableData: [],
      ids: []
    };
  },
  mounted() {
    // 分页
    this.BaseTable = this.$refs.table.$refs.pageTable;
    this.initSelect();
    // this.querySeries();
  },
  methods: {
    async initSelect() {
      // 服务顾问
      let testDrive = { role: '' };
      const res = await searchRoleByCode(testDrive);
      let data = arrayObjDeWeight(res.data, 'userId');
      console.log(res.data, '服务顾问122222222');
      this.columns.find(x => x.dataIndex === 'SERVICE_ADVISOR').filterItems = this.columns.find(x => x.dataIndex === 'SERVICE_ADVISOR').editItems = this.columns.find(
        x => x.dataIndex === 'RECEIVER'
      ).filterItems = this.columns.find(x => x.dataIndex === 'RECEIVER').editItems = data.map(x => ({
        text: x.employeeName,
        value: x.userId + ''
      }));
    },
    // async querySeries(){
    //   // 车系ID翻译
    //   const car = await getSeries();
    //   console.log(car,'车系ID翻译');
    //   this.columns.find(x => x.dataIndex === 'SERIES').editItems = car.data.map(x => ({ text: x.SERIES_CODE, value: x.SERIES_ID + '' }));
    //   this.columns.find(x => x.dataIndex === 'SERIES_NAME').editItems = car.data.map(x => ({ text: x.SERIES_NAME, value: x.SERIES_ID + '' }));

    // },

    createTopFilterList() {
      return [
        // {
        //   type: 'SELECT',
        //   label: '日期类型',
        //   fieldName: 'dateType',
        //   placeholder: '选择日期',
        //   itemList: [
        //     { text: '出库日期', value: 1001 },
        //     { text: '结算日期', value: 1002 }
        //   ],
        //   // rules: [{ required: true, message: '请输入日期类型', trigger: 'blur' }]
        // },
        {
          type: 'RANGE_DATE',
          label: '出库日期',
          fieldName: 'outStr|outEnd',
          placeholder: '选择日期',
          initialValue: [
            moment(new Date())
              .subtract(1, 'weeks')
              .format('YYYY-MM-DD'),
            moment(new Date()).format('YYYY-MM-DD')
            // 'YYYY-MM-DD HH:mm:ss'
          ],
          dateType: 'exactdaterange'
        },
        {
          type: 'RANGE_DATE',
          label: '结算日期',
          fieldName: 'balanceStr|balanceEnd',
          placeholder: '选择日期',
          dateType: 'exactdaterange'
        },
        {
          type: 'INPUT',
          label: '委托书编号',
          fieldName: 'roNo'
        },
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'partNo'
        },
        {
          type: 'INPUT',
          label: '配件名称',
          fieldName: 'partName'
        },
        {
          type: 'SELECT',
          label: '维修状态',
          itemList: this.createDictList(8049),
          fieldName: 'roStatus'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.$index + 1}</span>;
          }
        },
        {
          title: '委托书编号',
          dataIndex: 'RO_NO',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '牌照号',
          dataIndex: 'LICENSE',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '配件代码',
          dataIndex: 'PART_NO',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input',
          summationUnit: '品种',
          summation: true,
          summationDataIndex: 'count'
        },
        {
          title: '配件名称',
          dataIndex: 'PART_NAME',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '出库数量',
          dataIndex: 'PART_QUANTITY',
          sorter: true,
          filter: true,
          summation: true,
          showOverflowTooltip: true,
          precision: 2,
          filterType: 'input'
        },
        {
          title: '出库单价',
          dataIndex: 'PART_SALES_PRICE',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input',
          precision: 2
        },
        {
          title: '出库金额',
          dataIndex: 'PART_SALES_AMOUNT',
          sorter: true,
          summation: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'number',
          precision: 2,
          summationDataIndex: 'TOTAL_PART_SALES_AMOUNT'
        },
        {
          title: '出库日期',
          dataIndex: 'SEND_TIME',
          width: 150,
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'date-range',
          editable: false,
          editType: 'date-picker'
        },
        {
          title: '维修状态',
          dataIndex: 'RO_STATUS',
          sorter: true,
          editable: false,
          editType: 'select',
          editItems: this.createDictList(8049),
          filter: true,
          showOverflowTooltip: true,
          filterType: 'checkbox',
          filterItems: this.createDictList(8049)
        },
        {
          title: '领料人名称',
          dataIndex: 'RECEIVER',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'checkbox',
          editType: 'select',
          editItems: [],
          filterItems: []
        },
        {
          title: '结算日期',
          dataIndex: 'SQUARE_DATE',
          width: 150,
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'date-range',
          editable: false,
          editType: 'date-picker'
        },
        {
          title: '车系名称',
          dataIndex: 'SERIES_NAME',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input',
          editType: 'select',
          editItems: []
        },
        {
          title: '服务顾问',
          dataIndex: 'SERVICE_ADVISOR',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'checkbox',
          editType: 'select',
          editItems: [],
          filterItems: []
        }
      ];
    },
    selectData(val) {
      this.tableData = val;
    },
    changeHandle(val) {
      let searchData = _.cloneDeep(val);
      if (searchData.outStr) {
        searchData.outStr = searchData.outStr.slice(0, 10) + ' 00:00:00';
        searchData.outEnd = searchData.outEnd.slice(0, 10) + ' 23:59:59';
      }
      if (searchData.balanceStr) {
        searchData.balanceStr = searchData.balanceStr.slice(0, 10) + ' 00:00:00';
        searchData.balanceEnd = searchData.balanceEnd.slice(0, 10) + ' 23:59:59';
      }
      if ((!searchData.outStr || !searchData.outEnd) && (!searchData.balanceStr || !searchData.balanceEnd)) {
        return this.$notify.warning({ title: '提示信息', message: '请选择一种日期查询条件' });
      }
      // 增加日期限制
      if (searchData.outStr || searchData.outEnd) {
        // if (val.finishedDateBegin || val.finishedDateEnd) {
        let diffMonth = moment(val.outEnd).diff(moment(val.outStr), 'months');
        if (diffMonth > 5) return this.$notify.warning({ title: '提示信息', message: '起止日期跨度不能超过半年' });
        // }
      }
      if (searchData.balanceStr || searchData.balanceEnd) {
        let diffMonth = moment(val.balanceEnd).diff(moment(val.balanceStr), 'months');
        if (diffMonth > 5) return this.$notify.warning({ title: '提示信息', message: '起止日期跨度不能超过半年' });
      }

      this.fetchParams = Object.assign({}, searchData);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    }
  }
};
</script>

<style lang="less" scoped></style>
